package controllers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.aton.db.Config;
import com.sun.jmx.snmp.Timestamp;

import models.Operator;
import models.Oplog;
import models.SystemAuthority;
import models.User;
import services.AuthorityService;
import services.HraDetailService;
import services.UserInfoService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import utils.Validate;
import vos.HraDetailView;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.UserInfoSearchVo;

public class UserInfoController extends BaseController{
	private static final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 用户信息页面
	 *
	 * @Title: userInfo 
	 * @return: void
	 */
	public static void userInfo(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取用户信息列表
	 *
	 * @Title: getUserInfo 
	 * @return: void
	 */
	public static void getUserInfo(int page,int rows, String sort,String order,
			String idcard,String username,String phone){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		UserInfoSearchVo vo=new UserInfoSearchVo(page,rows,sort,order,idcard,username,phone);
		long total=UserInfoService.countUserInfo(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<User> lists=UserInfoService.listUserInfo(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 重置密码
	 * @Param idcard 身份证号
	 * @Title: resetPass 
	 * @return: void
	 */
	public static void resetPass(String idcard){
		if(!checkAuthority("USERINFO_RESET")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(idcard)){
			renderFailedJson(ReturnCode.ErrorCode, "身份证号不能为空");
		}
		Operator oper=getLoginUser();
		User user=User.findById(idcard);
		if(user==null){
			renderFailedJson(ReturnCode.ErrorCode, "用户不存在");
		}
		boolean isOk=UserInfoService.resetPass(user);
		if(isOk){
			Oplog oplog=new Oplog(oper.operatorid,"重置用户"+user.idcard+"密码","用户信息");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "重置密码成功");
		}
		else{
			renderFailedJson(ReturnCode.SuccessCode, "重置密码失败");
		}
	}
	/**
	 * 编辑用户
	 *
	 * @Title: edit 
	 * @return: void
	 */
	public static void edit(String id){
		if(StringUtils.isEmpty(id)){
			render("errors/404.html");
		}
		User user=User.findById(id);
		if(user==null){
			render("errors/404.html");
		}
		if(user.issmoke!=null&&user.issmoke){
			user.smoke="是";
		}
		else{
			user.smoke="否";
		}
		renderArgs.put("user", user);
		render();
	}
	/**
	 * 编辑用户数据
	 *
	 * @Title: updateUserInfo 
	 * @return: void
	 */
	public static void updateUserInfo(User user){
		if(!checkAuthority("USERINFO_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(user==null){
			renderFailedJson(ReturnCode.ErrorCode, "用户信息不能为空");
		}
		User oldUser=User.findById(user.idcard);
		if(oldUser==null){
			renderFailedJson(ReturnCode.ErrorCode, "修改的用户信息不存在");
		}
		if(StringUtils.isEmpty(user.idcard)){
			renderFailedJson(ReturnCode.ErrorCode, "用户身份证号不能为空");
		}
		if(StringUtils.isEmpty(user.phone)||!Validate.isMobile(user.phone)){
			renderFailedJson(ReturnCode.ErrorCode, "手机号格式不正确");
		}
		if(!Validate.isDate(user.birthdate)){
			renderFailedJson(ReturnCode.ErrorCode, "出生日期格式不正确");
		}
		Operator oper=getLoginUser();
		boolean isok=UserInfoService.updateUserInfo(user);
		if(isok){
			Oplog oplog=new Oplog(oper.operatorid,"编辑用户信息","用户信息");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 格式化日期
	 *
	 * @Title: formatBirthDate 
	 * @param birthdate
	 * @return
	 * @return: String
	 */
//	private static String formatBirthDate(String birthdate) {
//		// TODO Auto-generated method stub
//		String fdate="";
//		try{
//			Date date=dateFormater.parse(birthdate);
//			Calendar cal=Calendar.getInstance();
//			cal.setTime(date);
//			fdate=Integer.toString(cal.get(Calendar.DAY_OF_MONTH))+"."+
//					Integer.toString(cal.get(Calendar.MONTH))+"."+		
//					Integer.toString(cal.get(Calendar.YEAR));
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		return fdate;
//	}
	/**
	 * 根据用户身份证号获取体检数据
	 *
	 * @Title: getUserHra 
	 * @return: void
	 */
	public static void getUserHra(int page,int rows, String sort,String order,String idcard){
		if(StringUtils.isEmpty(idcard)){
			renderJSON(null);
		}
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		UserInfoSearchVo vo=new UserInfoSearchVo(page,rows,sort,order,idcard);
		long total=HraDetailService.countByIdCard(idcard);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<HraDetailView> lists=HraDetailService.listByIdCard(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param idcard 身份证
	 * @param username 用户名
	 * @param phone 手机号
	 * @return: void
	 */
	public static void exportXls(String username, String idcard, String phone){
		if(!checkAuthority("USERINFO_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		UserInfoSearchVo vo =new UserInfoSearchVo(username,idcard,phone);
		List<User> lists=UserInfoService.exportXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "用户信息"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出用户信息","用户信息");
		oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.userInfoXlsTemplate, lists), fileName);
	}
}
